Completed
Push — master ( a31174...af3b49 )
by Antonio
10s
created

inittable.js ➔ ... ➔ .createColumn   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
1
define(
2
    ['jquery', 'underscore'],
3
    function ($, _) {
4
        'use strict';
5
        return {
6
            init: function ($target, columns) {
7
8
                var $headerRow = $target.find('thead tr');
9
                if($headerRow[0].innerHTML && $headerRow[0].innerHTML.length != 0){
0 ignored issues
show
Best Practice introduced by
Comparing $headerRow.0.innerHTML.length to 0 using the != operator is not safe. Consider using !== instead.
Loading history...
10
                    return;
11
                }
12
13
                var $footerRow = $target.find('tfoot tr');
14
                var $tbody = $target.find('tbody');
15
                var values = $target.find('input.table-data').val();
16
17
                values = $.parseJSON(values?values:'{}');
18
19
                var emptyTitle = '<th class="AknGrid-headerCell" style="width: 47px"></th>';
20
                // Title for reorder column
21
                $headerRow.append(emptyTitle);
22
                $footerRow.append(emptyTitle);
23
                _.each(columns, function (column) {
24
                    var th = "<th class='AknGrid-headerCell "+column.id+"'>"+column.text+"</th>";
25
                    $headerRow.append(th);
26
                    $footerRow.append(th);
27
                }.bind(this));
0 ignored issues
show
unused-code introduced by
The call to bind does not seem necessary since the function does not use this. Consider calling it directly.
Loading history...
28
                // Title for delete button column
29
                $headerRow.append(emptyTitle);
30
                $footerRow.append(emptyTitle);
31
32
                _.each(values, function(row) {
33
                    var htmlColumns = [];
34
                    _.each(columns, function (column) {
35
                        var value = "";
36
                        if (column.id in row) {
37
                            value = row[column.id];
38
                        }
39
40
                        htmlColumns.push(this.createColumn(column, value));
41
                    }.bind(this));
42
                    $tbody.append(this.createRow(htmlColumns));
43
                }.bind(this));
44
            },
45
            createColumn: function (column, value) {
46
                var td =  $("<td class='AknGrid-bodyCell "+column.id+"' data-code='"+column.id+"'>"+column.func.renderField({column: column, value: value})+"</td>");
47
                column.func.init(td, column, value);
48
49
                return td;
50
            },
51
            createRow: function (htmlColumns) {
52
                var row = $('<tr class="flagbit-table-row AknGrid-bodyRow editable-item-row"></tr>');
53
                row.append($('<td class="AknGrid-bodyCell"><span class="handle"><i class="icon-reorder"></i></span></td>'));
54
                _.each(htmlColumns, function (htmlColumn) {
55
                    row.append(htmlColumn);
56
                });
57
                row.append($('<td class="AknGrid-bodyCell"><span class="btn btn-small AknButton AknButton--small AknIconButton--important delete-row"><i class="icon-trash"></i></span></td>'));
58
59
                return row;
60
            },
61
            createEmptyRow: function (columns) {
62
                var htmlColumns = [];
63
                _.each(columns, function (column) {
64
                    htmlColumns.push(this.createColumn(column, ''));
65
                }.bind(this));
66
67
                return this.createRow(htmlColumns);
68
            }
69
        };
70
    }
71
);
72